/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.dao;

import static cn.org.rapid_framework.util.ObjectUtils.isNotEmpty;

import java.util.HashMap;
import java.util.Map;

import javacommon.base.BaseHibernateDao;

import org.springframework.stereotype.Repository;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.tech15.pub.model.PubOffice;
import com.tech15.pub.vo.query.PubOfficeQuery;

@Repository
public class PubOfficeDao extends BaseHibernateDao<PubOffice,java.lang.String>{

	public Class getEntityClass() {
		return PubOffice.class;
	}
	
	public Page findByPageRequest(PageRequest<Map> pageRequest) {
		String sql = "select t from PubOffice t where 1=1 "
				+ "/~ and t.name = '[name]' ~/"
				+ "/~ and t.lb = '[lb]' ~/"
				+ "/~ and t.memo = '[memo]' ~/"
				+ "/~ and t.uptime = '[uptime]' ~/"
				+ "/~ order by [sortColumns] ~/";
		return pageQuery(sql,pageRequest);
	}
	
	public Page findPage(PubOfficeQuery query) {
		String sql = "select t.*,t1.name as categoryName,t2.name as dataname from Pub_Office t " +
				" left join pub_data t1 on t.lb=t1.id " +
				" left join pub_data t2 on t.lb2=t2.id where 1=1 ";
		if(isNotEmpty(query.getUserid())){
			sql += " and (t.isauth=0 or (select count(*) from pub_office_auth a where a.poid=t.id and a.userid="+query.getUserid()+") >0) ";
		}
		if(isNotEmpty(query.getName())) {
			sql += " and t.name like '%"+query.getName()+"%'";
		}
		if(isNotEmpty(query.getLb())) {
			sql += " and t.lb = "+query.getLb();
		}
		if(isNotEmpty(query.getLb2())) {
			sql += " and t.lb2 = "+query.getLb2();
		}
		if(isNotEmpty(query.getUptimeBegin())) {
			sql += " and to_char(t.uptime,'yyyy-mm-dd') = '"+query.getUptimeBegin()+"'";
		}
		sql += " order by t.uptime desc ";
		Map map = new HashMap();
		map.put("id", "String");
		map.put("name", "String");
		map.put("lb", "Integer");
		map.put("memo", "String");
		map.put("uptime", "Timestamp");
		map.put("categoryName", "String");
		map.put("dataname", "String");
		map.put("fbbm","String");
		return pageSqlQuery(sql, query, map, PubOffice.class);
	}

	public Page searchIndex(PubOfficeQuery query) {
		String sql = "select t.*,t1.name as categoryName,t2.name as dataname from Pub_Office t " +
				" left join pub_data t1 on t.lb=t1.id " +
				" left join pub_data t2 on t.lb2=t2.id where " +
				" (select count(*) from pub_office b where b.lb=t.lb and b.uptime>t.uptime) <3  ";
		if(isNotEmpty(query.getUserid())){
			sql += " and (t.isauth=0 or (select count(*) from pub_office_auth a where a.poid=t.id and a.userid="+query.getUserid()+") >0) ";
		}
		if(isNotEmpty(query.getName())) {
			sql += " and t.name like '%"+query.getName()+"%'";
		}
		if(isNotEmpty(query.getLb())) {
			sql += " and t.lb = "+query.getLb();
		}
		if(isNotEmpty(query.getLb2())) {
			sql += " and t.lb2 = "+query.getLb2();
		}
		if(isNotEmpty(query.getUptimeBegin())) {
			sql += " and to_char(t.uptime,'yyyy-mm-dd') = '"+query.getUptimeBegin()+"'";
		}
		sql += " order by t.uptime desc ";
		Map map = new HashMap();
		map.put("id", "String");
		map.put("name", "String");
		map.put("lb", "Integer");
		map.put("memo", "String");
		map.put("uptime", "Timestamp");
		map.put("categoryName", "String");
		map.put("dataname", "String");
		map.put("fbbm", "String");
		return pageSqlQuery(sql, query, map, PubOffice.class);
	}
	

}
